package cn.seaboot.admin.starter.converter;

import cn.seaboot.commons.core.Converter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 格林威治标准时间（Greenwich Mean Time，简称 GMT）
 * Tue, 29 Nov 2022 03:30:28 GMT
 * <p>
 * 世界协调时（Coordinated Universal Time，简称 UTC）
 * Fri, 08 Nov 2002 09:42:22 +0800
 * <p>
 * 中国标准时间（CST）
 * Mon Oct 26 15:19:15 CST 2022
 * <p>
 * In Europe, because the number of countries is large, different country need different time zone, the time zone is very important.
 * <p>
 * But In China, every where always use Beijing time, in most OA systems, it never seem that used other time zone.
 * the date usually be processed as a simple string when developing.
 * <p>
 * If you developing transnational applications(hotel, train, air tickets), you should know something about time zone.
 * <p>
 * This configuration is design for chinese developer.
 *
 * @author Mr.css
 * @version 2020-03-06 10:16
 */
@Configuration
public class ConverterConfig {

    /**
     * spring-integration-core 包中有个冲突的 integrationConversionService
     */
    @Resource
    @Qualifier("mvcConversionService")
    private GenericConversionService conversionService;

    @Resource
    private ConverterProperties converterConstant;

    /**
     * 配置失败直接Error，终止程序运行
     */
    @PostConstruct
    public void init() {
        if (conversionService != null) {
            if ("java.time".equals(converterConstant.getTimeType())) {
                //  if use java.time api
                Converter.useJava8(conversionService);
            } else if ("java.sql".equals(converterConstant.getTimeType())) {
                //  if use java.sql api
                Converter.useSql(conversionService);
            }
            //  maybe you need deal with the numbers in scientific notation
            if (converterConstant.isScientificNotation()) {
                Converter.useScientificNotation(conversionService);
            }
        }
    }
}
